package com.xt.purchasingsystem.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xt.purchasingsystem.dto.QualityDto;
import com.xt.purchasingsystem.dto.SGatherDto;
import com.xt.purchasingsystem.feign.ErpManagementClient;
import com.xt.purchasingsystem.mapper.QualityDetailMapper;
import com.xt.purchasingsystem.mapper.QualityMapper;
import com.xt.purchasingsystem.mapper.ReleaseCargoDetailMapper;
import com.xt.purchasingsystem.mapper.ReleaseCargoMapper;
import com.xt.purchasingsystem.model.*;
import com.xt.purchasingsystem.service.QualityService;
import com.xt.purchasingsystem.utils.IdWorker;
import com.xt.purchasingsystem.utils.Result;
import com.xt.purchasingsystem.utils.ResultFactory;
import com.xt.purchasingsystem.utils.SerialNumberUtil;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yhp
 * @since 2020-05-31
 */
@Service
public class QualityServiceImpl extends ServiceImpl<QualityMapper, Quality> implements QualityService {

    @Autowired
    private IdWorker idWorker;
    @Autowired
    private ErpManagementClient erpManagementClient;
    @Autowired
    private QualityMapper qualityMapper;
    @Autowired
    private ReleaseCargoMapper releaseCargoMapper;
    @Autowired
    private ReleaseCargoDetailMapper releaseCargoDetailMapper;
    @Autowired
    private QualityDetailMapper qualityDetailMapper;
    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Transactional
    @Override
    public Result saveQualityAndDetail(QualityDto qualityDto) {
        CompletableFuture.runAsync(() -> {
            qualityDto.getReleaseCargoDto().getReleaseCargo().setQualityTag("Q001-1");
            releaseCargoMapper.updateById(qualityDto.getReleaseCargoDto().getReleaseCargo());
        }, threadPoolExecutor);

        String id = String.valueOf(idWorker.nextId());

        CompletableFuture.runAsync(() -> {
            qualityDto.getQuality().setId(id);
            qualityMapper.insert(qualityDto.getQuality());
        }, threadPoolExecutor);

        for (QualityDetail qualityDetail : qualityDto.getQualityDetailList()) {
            qualityDetail.setId(String.valueOf(idWorker.nextId()));
            qualityDetail.setQualityId(id);
            qualityDetailMapper.insert(qualityDetail);

            if (qualityDetail.getThisNumber() < qualityDetail.getBuyerNumber()) {
                String rid = String.valueOf(idWorker.nextId());

                ReleaseCargo releaseCargo = qualityDto.getReleaseCargoDto().getReleaseCargo();

                releaseCargo.setId(rid);
                releaseCargo.setChecker("");
                releaseCargo.setCheckTime(null);
                releaseCargo.setCheckTag("R001-0");
                releaseCargo.setQualityTag("Q001-0");
                releaseCargo.setRegister(qualityDto.getQuality().getRegister());
                releaseCargo.setRegisterTime(qualityDto.getQuality().getRegisterTime());
                Double number = qualityDetail.getBuyerNumber() - qualityDetail.getThisNumber();
                releaseCargo.setSumNumber(qualityDetail.getBuyerNumber() - qualityDetail.getThisNumber());
                releaseCargo.setSumMoney((qualityDetail.getBuyerNumber() - qualityDetail.getThisNumber()) * qualityDto.getReleaseCargoDto().getReleaseCargoDetails().get(0).getPrice());

                qualityDto.getReleaseCargoDto().getReleaseCargoDetails().get(0).setId(String.valueOf(idWorker.nextId()));
                qualityDto.getReleaseCargoDto().getReleaseCargoDetails().get(0).setReleaseCargoId(rid);
                qualityDto.getReleaseCargoDto().getReleaseCargoDetails().get(0).setNeedNumber(number);
                qualityDto.getReleaseCargoDto().getReleaseCargoDetails().get(0).setPutNumber(number);
                qualityDto.getReleaseCargoDto().getReleaseCargoDetails().get(0).setPutSubtotal(number * qualityDto.getReleaseCargoDto().getReleaseCargoDetails().get(0).getPrice());
                releaseCargoMapper.insert(releaseCargo);
                releaseCargoDetailMapper.insert(qualityDto.getReleaseCargoDto().getReleaseCargoDetails().get(0));

            }
        }

        return ResultFactory.buildSuccessResult("提交成功,需要审核!");
    }

    @Transactional
    @Override
    public Result getAllNoCheckQualityAndDetails(Integer currNo, Integer pageSize) {
        PageHelper.startPage(currNo, pageSize);
        List<QualityDto> allBuyerExecuteNoCheck = qualityMapper.getAllNoCheckQualityAndDetails();
        PageInfo<QualityDto> pageInfo = new PageInfo(allBuyerExecuteNoCheck);
        return ResultFactory.buildSuccessResult("查询成功", pageInfo);
    }

    @Transactional
    @Override
    public Result checkQuality(QualityDto qualityDto) {
        qualityMapper.updateById(qualityDto.getQuality());


        CompletableFuture.runAsync(() -> {
            SGatherDto sGatherDto = new SGatherDto();

            SGather sGather = new SGather();
            List<SGatherDetails> sgatherDetails = new ArrayList<>();

            sGather.setGatherId(SerialNumberUtil.godownEntrySerNumber());
            sGather.setStorer(qualityDto.getQuality().getChecker());
            sGather.setReason("R001-2");
            QualityDetail qualityDetail = qualityDto.getQualityDetailList().get(0);

            sGather.setAmountSum(BigDecimal.valueOf(qualityDetail.getThisNumber()));
            sGather.setCostPriceSum(BigDecimal.valueOf(qualityDetail.getThisNumber() * qualityDetail.getPrice()));

            sGather.setRegister(qualityDto.getQuality().getChecker());
            sGather.setRegisterTime(qualityDto.getQuality().getCheckTime());
            sGather.setCheckTag("S001-0");
            sGather.setStoreTag("K002-1");

            SGatherDetails sGatherDetails = new SGatherDetails();
            sGatherDetails.setProductId(qualityDetail.getProductId());
            sGatherDetails.setProductName(qualityDetail.getProductName());
            if (qualityDetail.getProductDescribe() != null && qualityDetail.getProductDescribe().length() > 0) {
                sGatherDetails.setProductDescribe(qualityDetail.getProductDescribe());
            }

            if (qualityDetail.getUnit() != null && qualityDetail.getUnit().length() > 0) {
                sGatherDetails.setAmountUnit(qualityDetail.getUnit());
            }
            sGatherDetails.setAmount(BigDecimal.valueOf(qualityDetail.getThisNumber()));
            sGatherDetails.setCostPrice(BigDecimal.valueOf(qualityDetail.getPrice()));
            sGatherDetails.setSubtotal(BigDecimal.valueOf(qualityDetail.getPrice() * qualityDetail.getThisNumber()));
            sGatherDetails.setGatherTag("K002-1");

            sgatherDetails.add(sGatherDetails);
            sGatherDto.setSgather(sGather);
            sGatherDto.setSgatherDetails(sgatherDetails);
            erpManagementClient.submitWarehouse(sGatherDto);
        }, threadPoolExecutor);

        return ResultFactory.buildSuccessResult("审核成功");

    }
}
